function route = hadlockretrace(M,s,t,d)

  route = t;

  dtour = d;
  waitlist = [-1, 0; 0, -1; 1, 0; 0, 1];

  Mtemp = M;
  Mtemp(t(1),t(2)) = -1;
  found = 0;
  posnow = t;

  while 1
    poslist = repmat(posnow,4,1) + waitlist;
    candi = posnow;

    for k = 1:size(poslist,1)
      a = poslist(k,:);

      if min(a) < 1 || sum(a > size(M)) > 0
          continue;
      end

      n = Mtemp(a(1),a(2));

      if n < 0
          continue;
      end

      Mtemp(a(1),a(2)) = -1;

      if sum(a==s) == 2
        route = [a; route];
        found = 1;
        break;
      elseif (n < dtour) && (n >= 0)
        dtour = n;
        candi = a;
      elseif (n == dtour) && (n >= 0)
        if sum(posnow == candi) == 2
            candi = a;
        elseif distman(s,a) + 1 == distman(s, posnow)
            candi = a;
        end
        break;
      end
    end

    if found == 1
      break;
    end

    posnow = candi;
    route = [posnow; route];

  end

end
